Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SomeModel._default_manager return a BaseManager[SomeModel] instead of BaseManager[Model] #817

Merged
merged 6 commits into from
Jan 28, 2022

Conversation

emma-blossom
Copy link
Contributor

This changes the type annotations for _default_manager (and _base_manager) such that the returned manager's model is the specific model the manager belongs to instead of django.db.models.base.Model

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Great feature, please, see my comments

class classproperty(Generic[_Get]):
fget: Optional[Callable[[Type[_Self]], _Get]] = ...
def __init__(self, method: Optional[Callable[[Type[_Self]], _Get]] = ...) -> None: ...
def __get__(self, instance: Any, cls: Type[_Self] = ...) -> _Get: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I see __get__ is usually an overload of two functions.
You can take a look here: https://github.com/python/typeshed/

@@ -48,7 +48,7 @@
class Base(Generic[_T]):
def __init__(self, model_cls: Type[_T]):
self.model_cls = model_cls
reveal_type(self.model_cls._default_manager) # N: Revealed type is "django.db.models.manager.BaseManager[django.db.models.base.Model]"
reveal_type(self.model_cls._default_manager) # N: Revealed type is "django.db.models.manager.BaseManager[_T`1]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also test ._base_manager of some Model, so we can ensure that _T is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm super unfamiliar with these types of tests. I hope the new test makes sense!

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sobolevn sobolevn merged commit 7329059 into typeddjango:master Jan 28, 2022
@clouds56
Copy link
Contributor

I'm using pylance and found this does not work.

user: Users = Users._default_manager.get(pk=user_id)

shows error:

(property) _default_manager: BaseManager[_Self@_default_manager]
Expression of type "_Self@_default_manager" cannot be assigned to declared type "Users"
"Model*" is incompatible with "Users" Pylance(reportGeneralTypeIssues)

any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants